---
title: Validate a document
summary: Guards that a Document loads successfully and contains valid PDF bytes.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-17
library: PdfAPI.Core
reference_type: package
operation_id: OP-02
trigger: PdfClientService.ValidatePdf(Document, PdfOptions) invoked
---

# Validate a document

This operation extends [Validate PDF bytes](op-01-validate-pdf-bytes.md) to a `Document`-level guard: it requires a non-null document, loads its bytes, optionally deletes the source file, then re-runs the byte-level PDF signature check.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-02 |
| **Trigger** | Caller invokes `ValidatePdf(Document document, PdfOptions options)` |
| **Inputs** | `Document`, `PdfOptions` |
| **Outputs** | Returns without a value on success; throws `PdfException` on failure; may delete the source file as a side effect |
| **Primary components** | `PdfAPI.Core.PdfClientService.ValidatePdf(Document, PdfOptions)` |
| **Entry point** | `PdfAPI.Core\Services\PdfClientService.cs` |

## Steps

1. If `document` is null, throw `PdfException("No document was specified.")`. ([BR-04](../business-rules.md#br-04-document-validation-requires-a-non-null-document))
   - On failure: aborts before any file I/O occurs.
2. Call `document.Load()` to materialise bytes from `File` or from `FilePath`. ([BR-05](../business-rules.md#br-05-document-load-requires-in-memory-or-on-disk-bytes))
   - Delegates to [Load a document](op-09-load-a-document.md).
3. If the load failed, throw `PdfException("The document could not be loaded.")`. ([BR-06](../business-rules.md#br-06-failed-document-load-aborts-validation))
4. If `options` is null or `options.CleanupInputFile` is true, call `document.Delete()` to remove the on-disk source file. ([BR-07](../business-rules.md#br-07-input-file-cleanup-after-load))
   - Delegates to [Persist and remove a document](op-10-persist-and-remove-a-document.md).
   - This deletion happens **before** step 5's signature check, so a document that later fails validation has already lost its on-disk copy.
5. Run [Validate PDF bytes](op-01-validate-pdf-bytes.md) against the in-memory bytes. ([BR-01](../business-rules.md#br-01-pdf-signature-validation), [BR-02](../business-rules.md#br-02-null-bytes-are-never-a-pdf), [BR-03](../business-rules.md#br-03-invalid-pdf-input-aborts-with-pdfexception))

## Process flow

The following diagram visualizes this operation's control flow.

```mermaid
flowchart TD
	Start(["ValidatePdf(document, options)"]) --> Null{document null?}
	Null -->|Yes| ErrNull(["Throw PdfException:<br>No document was specified"])
	Null -->|No| Load["document.Load()"]
	Load --> Loaded{Loaded?}
	Loaded -->|No| ErrLoad(["Throw PdfException:<br>could not be loaded"])
	Loaded -->|Yes| Cleanup{options null or<br>CleanupInputFile?}
	Cleanup -->|Yes| Delete["document.Delete()"]
	Cleanup -->|No| Validate
	Delete --> Validate["OP-01: ValidatePdf(bytes)"]
	Validate -->|Invalid| ErrInvalid([Throw PdfException])
	Validate -->|Valid| Ok([Return])
```

## Failure and edge-case behavior
- Deleting the source file before validating its content means rejected input files are unrecoverable after this operation runs ([BR-07](../business-rules.md#br-07-input-file-cleanup-after-load)).
- A null `PdfOptions` is treated identically to `CleanupInputFile = true`.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)
- [Security and authorization](../security-authorization.md)

## Sources
- `PdfAPI.Core\Services\PdfClientService.cs`
- `PdfAPI.Core\Document.cs`
- `PdfAPI.Core\PdfOptions.cs`
